package com.example.springboot_zc.controller;

import com.example.springboot_zc.pojo.ApiMsg;
import com.example.springboot_zc.pojo.User;
import com.example.springboot_zc.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/img")
public class imgController {

    @Autowired
    private userService userService;

    @PostMapping(value = "/upload")
    public String upload(@RequestParam("picture") MultipartFile file,@RequestParam("maxZIndex") String maxZIndex,@RequestParam("userNum") String userNum) throws IOException {
        System.out.println(maxZIndex+"   "+userNum);
        System.out.println(file);
        if (ObjectUtils.isEmpty(file)) {
            System.out.println("文件是空的");
            return "null";
        }
        String filename = file.getOriginalFilename();// 文件的名字
        int pointLastIndex=filename.lastIndexOf('.');
        filename=filename.substring(pointLastIndex,filename.length());
        File descFile = new File("C:\\Users\\18361\\Desktop\\vue\\1\\vue_zc-master\\vue_zc-master\\src\\static\\"+userNum+'_'+maxZIndex+filename);//如果不确定位置可以使用绝对路径进行测试
        if (!descFile.exists()) {
            descFile.createNewFile();
        }
        try {
            file.transferTo(descFile); // 文件保存
        } catch (Exception e) {
            System.out.println("文件保存出现异常了");
            e.printStackTrace();
            return "null";
        }
        System.out.println("文件上传成功了");
        System.out.println(filename);

        filename = URLEncoder.encode(filename,"utf-8");

        return "redirect:/showImg.html"+"?filename="+filename;
    }

}
